package gui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.graphics.Point;

import particle_simulator_chem.World;

public class WorldSettings extends Dialog {

	protected Object result;
	protected Shell shlWorldSet;
	private Text txtGP;
	private Text txtGX;
	private Text txtGY;
	private Text txtGZ;
	private int x;
	private int y;
	World world;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public WorldSettings(Shell parent, int style, World world, int x, int y) {
		super(parent, style);
		setText("World Settings");
		this.x = x;
		this.y = y;
		this.world = world;
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shlWorldSet.open();
		shlWorldSet.layout();
		Display display = getParent().getDisplay();
		while (!shlWorldSet.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shlWorldSet = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.RESIZE);
		shlWorldSet.setMinimumSize(new Point(350, 206));
		shlWorldSet.setLocation(this.x, this.y);
		shlWorldSet.setImage(SWTResourceManager.getImage(WorldSettings.class, "/particle_simulator_chem/BinaryBirdIconV.1.4.png"));
		shlWorldSet.setSize(350, 206);
		shlWorldSet.setText(getText());
		shlWorldSet.setLayout(null);
		
		final Group grpGravSet = new Group(shlWorldSet, SWT.NONE);
		grpGravSet.setBounds(10, 10, 320, 62);
		grpGravSet.setText("Gravity Settings");
		grpGravSet.setLayout(null);
		
		Label lblGP = new Label(grpGravSet, SWT.NONE);
		lblGP.setFont(SWTResourceManager.getFont("Sans", 10, SWT.NORMAL));
		lblGP.setBounds(10, 20, 15, 15);
		lblGP.setText("GP");
		
		Label lblGX = new Label(grpGravSet, SWT.NONE);
		lblGX.setBounds(10, 40, 15, 15);
		lblGX.setText("GX");
		
		Label lblGY = new Label(grpGravSet, SWT.NONE);
		lblGY.setBounds(160, 20, 15, 15);
		lblGY.setText("GY");
		
		txtGP = new Text(grpGravSet, SWT.BORDER); //default gravity strength
		txtGP.setBounds(30, 20, 125, 18);
		txtGP.setText(Double.toString(world.getDefaultGravity()));
		
		txtGX = new Text(grpGravSet, SWT.BORDER); //default gravity x
		txtGX.setBounds(30, 38, 125, 18);
		txtGX.setText(Double.toString(world.getDefaultGX()));
		
		txtGY = new Text(grpGravSet, SWT.BORDER); //default gravity y
		txtGY.setBounds(180, 20, 125, 18);
		txtGY.setText(Double.toString(world.getDefaultGY()));
		
		Label lblGz = new Label(grpGravSet, SWT.NONE);
		lblGz.setBounds(160, 40, 15, 15);
		lblGz.setText("GZ");
		
		txtGZ = new Text(grpGravSet, SWT.BORDER);
		txtGZ.setText("0.0");
		txtGZ.setBounds(180, 38, 125, 18);
		
		final Group grpIllustrationSet = new Group(shlWorldSet, SWT.BORDER);
		grpIllustrationSet.setBounds(10, 78, 320, 52);
		grpIllustrationSet.setLayout(null);
		grpIllustrationSet.setText("Illustration Settings");
		
		final Spinner spnBoidSize = new Spinner(grpIllustrationSet, SWT.BORDER);
		if(world.getIllustrationShape() != 1) {
			spnBoidSize.setEnabled(false);
		}
		spnBoidSize.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				world.setIllustrationRadius(spnBoidSize.getSelection());
			}
		});
		spnBoidSize.setMaximum(50);
		spnBoidSize.setSelection(world.getIllustrationRadius());
		spnBoidSize.setBounds(251, 19, 55, 22);
		
		Label lblBoidSize = new Label(grpIllustrationSet, SWT.NONE);
		lblBoidSize.setBounds(190, 22, 55, 15);
		lblBoidSize.setText("Boid Size");
		
		Label vspIllustrationSettings = new Label(grpIllustrationSet, SWT.SEPARATOR | SWT.VERTICAL);
		vspIllustrationSettings.setBounds(181, 8, 2, 40);
		
		CCombo comboIllustrationMedium = new CCombo(grpIllustrationSet, SWT.BORDER);
		comboIllustrationMedium.setToolTipText("Select Illustration Method");
		comboIllustrationMedium.setListVisible(true);
		comboIllustrationMedium.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		comboIllustrationMedium.setEditable(false);
		comboIllustrationMedium.setItems(new String[] {"Point", "Circle", "Symbol", "Int Coords", "Double Coords"});
		comboIllustrationMedium.setBounds(10, 20, 160, 20);
		
		final Composite compFinalButtons = new Composite(shlWorldSet, SWT.NONE);
		compFinalButtons.setBounds(128, 136, 202, 25);
		
		Button btnApply = new Button(compFinalButtons, SWT.NONE);
		btnApply.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				try{
				world.setGravity(Double.parseDouble(txtGP.getText()));
				world.setGravX(Double.parseDouble(txtGX.getText()));
				world.setGravY(Double.parseDouble(txtGY.getText()));
				//world.resetGravity();
				}
				catch(NumberFormatException nfe){
					txtGP.setText(Double.toString(world.getDefaultGravity()));
					txtGX.setText(Double.toString(world.getDefaultGX()));
					txtGY.setText(Double.toString(world.getDefaultGY()));
				}
			}
		});
		btnApply.setBounds(128, 0, 75, 25);
		btnApply.setText("Apply");
		
		Button btnRestoreToDefaults = new Button(compFinalButtons, SWT.NONE);
		btnRestoreToDefaults.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				txtGP.setText(Double.toString(world.getDefaultGravity()));
				txtGX.setText(Double.toString(world.getDefaultGX()));
				txtGY.setText(Double.toString(world.getDefaultGY()));
			}
		});
		btnRestoreToDefaults.setBounds(0, 0, 122, 25);
		btnRestoreToDefaults.setText("Restore To Defaults");
		
		shlWorldSet.addControlListener(new ControlAdapter() {
			@Override
			public void controlResized(ControlEvent e) {
				Rectangle shellBounds = shlWorldSet.getBounds();
				int width = shellBounds.width;
				int height = shellBounds.height;
				int heightNoConstants = height - 92;
				
				grpGravSet.setBounds(10, 10, width - 30, (int) ((double)heightNoConstants * ((double) 62 / (double) 114)));
				
				grpIllustrationSet.setBounds(10, 16 + grpGravSet.getBounds().height, width - 30, (int) ((double) heightNoConstants * ((double) 52 / (double) 114)));
				
				Rectangle cfbBounds = compFinalButtons.getBounds();
				compFinalButtons.setBounds(width - 20 - cfbBounds.width, height - 45 - cfbBounds.height, 202, 25);
			}
		});
	}
	
	public void setLocation(int x, int y){
		this.shlWorldSet.setLocation(x, y);
	}
}
